
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:geomancy/enums/index.dart';
import 'package:geomancy/providers/gallery_provider.dart';
import 'package:geomancy/styles/index.dart';
import 'package:geomancy/widgets/gallery_util/index.dart';
import 'package:geomancy/widgets/modal_util/index.dart';
import 'package:geomancy/widgets/permission_reson_modal.dart';
import 'package:go_router/go_router.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:provider/provider.dart';

class GalleryPhotographWidget extends StatefulWidget {
  const GalleryPhotographWidget({super.key});

  @override
  State<GalleryPhotographWidget> createState() => _GalleryPhotographWidgetState();
}

class _GalleryPhotographWidgetState extends State<GalleryPhotographWidget> {

  //拍照
  Future takePhoto() async {
    GalleryUtils.getCameraPermission(context: context, success: () async {
      final ImagePicker picker = ImagePicker();
      PermissionReasonModal.cancel();
      final XFile? result = await picker.pickImage(source: ImageSource.camera);
      if (result != null) {
        final AssetEntity photo = await PhotoManager.editor.saveImageWithPath(result.path, title: result.path);
        // if (!mounted && photo != null) return;
        // final scene = context.read<GalleryProvider>().getGalleryScene;
        // if (scene == GalleryScene.cover) {
        //   final cropAspectRatio = context.read<GalleryProvider>().getCropAspectRatio;
        //   ModalUtil.showGeneralDialogWidget(
        //     context: context,
        //     modalWidget: CropPhotoPage(entity: photo, cropAspectRatio: cropAspectRatio),
        //   );
        // } else if (scene == GalleryScene.avatar || scene == GalleryScene.registerAvatar) {
        //   ModalUtil.showGeneralDialogWidget(
        //       context: context,
        //       modalWidget: CropAvatarPage(entity: photo!)
        //   );
        // } else {
        //   context.read<GalleryProvider>().addAllGalleryMediaList(photo);
        //   context.read<GalleryProvider>().addTmpSelectedList(photo);
        // }
      }
    }, failed: (bool value) {
      if (value) {
        ModalUtil.showCustomDialog(
          context: context,
          title: '你还没开启相机权限',
          confirmTxt: '去开启',
          confirmHandle: () {
            context.pop();
            openAppSettings();
          }
        );
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: takePhoto,
      child: Container(
        width: 200.w,
        height: 200.w,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          border: Border.all(width: 6.w, color: ColorStyle.borderColor),
            borderRadius: BorderRadius.all(Radius.circular(30.w))
        ),
        child: Image.asset('assets/icons/icon_gallery_camera.png', width: 56.w),
      ),
    );
  }
}
